import requests
import json

# 用户输入姓名/手机号/身份证
sech_name = input("请输入姓名/手机号/身份证:")
# 用户选择查询的年份
year = input("请输入查询年份(例如:2022, 2023, 2024):")

# 构造请求数据
data = {
    "areaList": [],
    "creTime": int(year),
    "checkType": [1, 2, 3, 4, 5, 6, 7, 8, 9],
    "lawmanId": "null",
    "sechName": sech_name,
    "offset": 0,
    "limit": 10
}

# 转换为JSON格式
json_data = json.dumps(data)

# 指定请求的URL
url = "https://wx.hnocc.com/newMechanusmData/queryPersonalPage"

# 发送POST请求
headers = {'Content-Type': 'application/json'}
response = requests.post(url, data=json_data, headers=headers)

# 检查响应状态码
if response.status_code == 200:
    # 将响应内容解析为JSON
    try:
        response_data = response.json()
        
        # 检查是否存在rows字段，并且rows字段是列表
        if 'rows' in response_data and isinstance(response_data['rows'], list):
            for item in response_data['rows']:
                # 截取所需的信息
                medical_institution_name = item.get('medicalInstitutionName', '检查机构未提供')
                name = item.get('name', '姓名未提供')
                id_card = item.get('idCard', '身份证未提供')
                phone = item.get('phone', '手机号未提供')
                detect_time = item.get('detectTime', '办证时间未提供')
                id_card_addr = item.get('idCardAddr', '地址未提供')
                
                # 打印截取的信息
                print(f"检查机构: {medical_institution_name}")
                print(f"姓名: {name}")
                print(f"身份证: {id_card}")
                print(f"手机号: {phone}")
                print(f"办证时间: {detect_time}")
                print(f"地址: {id_card_addr}")
                print("-" * 40)  # 分隔线
                
                # 提示用户是否保存照片
                save_photo = input("是否获取大头？(yes/no): ").lower()
                if save_photo == 'yes':
                    save_photo_url = "https://wx.hnocc.com/supervision/queryPersonalList"
                    photo_data = {
                        "sechName": id_card,
                        "openid": "自己抓",
                        "limit": 10,
                        "offset": 0
                    }
                    photo_response = requests.post(save_photo_url, json=photo_data, headers=headers)
                    if photo_response.status_code == 200:
                        photo_data = photo_response.json()
                        if 'data' in photo_data and 'rows' in photo_data['data'] and isinstance(photo_data['data']['rows'], list):
                            for photo_item in photo_data['data']['rows']:
                                head_img = photo_item.get('headImg', '无照片')
                                if head_img:
                                    # 下载图片并保存到本地
                                    photo_content = requests.get(head_img).content
                                    with open(f"photo_{id_card}.jpg", "wb") as photo_file:
                                        photo_file.write(photo_content)
                                        print(f"照片已保存为 photo_{id_card}.jpg")
                                else:
                                    print("无照片信息")
                        else:
                            print("照片信息获取失败")
                    else:
                        print("照片保存请求失败，状态码:", photo_response.status_code)
        else:
            print("响应数据中不存在rows字段或rows不是列表")
    except json.JSONDecodeError:
        print("响应内容不是有效的JSON格式")
else:
    print("请求失败，状态码:", response.status_code)